org 0x7c00
[bits 16]
xor ax, ax 
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov sp, 0x7c00 ;清零操作
dira db 0
dirb db 2
tema db 12
temb db 79 ;设置变量初始值，后面会改变
alt db '0'

mov dh, 10
mov dl, 79
mov ah, 6 ;向上滚屏
int 10h

mov dh, 3
mov dl, 0    ;设置位置
mov ah, 2
int 10h
mov al, '0'
xor bl, bl
mov cx, 80
mov ah, 9  ;bl可以设置一些颜色
int 10h
mov dh, 7
mov dl, 0
mov ah, 2   ;设置位置
int 10h
mov ah, 9
int 10h

mov ah, 02H
xor bh, bh
mov dh, 0
mov dl, 0
int 10h

mov ah, 09H
mov cx, 2
mov al, '2'
mov bl, 01h   ;输出两个2
int 10h

mov ah, 02H
mov dl, 2
int 10h

mov ah, 09H
mov al, '3'    ;输出两个3
int 10h

mov ah, 02H
mov dl, 4
int 10h

mov ah, 09H
mov cx, 1
mov al, '6'
int 10h

mov ah, 02H
mov dl, 5
int 10h

mov ah, 09H
mov cx, 1
mov al, '2'
int 10h

mov ah, 02H
mov dl, 6
int 10h

mov ah, 09H
mov cx, 1
mov al, '5'
int 10h

mov ah, 02H
mov dl, 7
int 10h

mov ah, 09H
mov cx, 1
mov al, '8'
int 10h

;以下才是正文，实现小球功能

mov dh, 12
mov dl, 0
xor bh, bh
mov ah, 2
int 10h     ;调整光标
mov bl, 00001111b
mov al, [alt]    ;刚开始是有值的，为0





while:
mov cx, 1
mov ah, 9
int 10h

mov al, [dira]    
;妙手，这里主要的作用是存储al返回来的数字（123）
;由此确立小球运动状态
call move
mov [dira], al

mov ah, 2
int 10h

call diao
call tiao

mov cx, 1
mov ah, 9
int 10h

mov al, [dirb]
call move
mov [dirb], al

mov ah, 2
int 10h

xor ax, ax
mov ah, 86h
mov cx, 2
int 15h

call diao
call tiao
jmp while







tiao:  ;仅仅更改[alt]的值
mov al, [alt]
cmp al, '9'
je app
inc al
mov [alt], al
ret
app:
mov al, '0'
mov [alt], al
ret


diao:
mov al, bl
or al, 11110000b
cmp al, 11110000b
jne di1
add bl, 00010000b
di1:
sub bl, 1
mov al, bl
or al, 10001111b
cmp al, 11111111b
jne di2
and bl, 10001111b
ret
di2:
add bl, 00010000b
ret


move:
cmp al, 0   ;0代表右下，1代表右上，2代表左上，默认3表左下
je m1
cmp al, 1
je m2
cmp al, 2
je m3          ;分为0 1 2 3四种情况
dec dl         ;默认情况，向左下角移动（ah是行，al是列）
inc dh
cmp dh, 24
je d7
cmp dl, 0
je d8
jmp m4
d7:
mov al, 2
jmp m4
d8:
mov al, 0
jmp m4
m1:            ;向右下角移动
inc dh
inc dl
cmp dh, 24   ;撞到下边
je d1
cmp dl, 79  ;撞到右边
je d2
jmp m4    ;m4代表很幸运啥也没撞到
d1:
mov al, 1
jmp m4
d2:
mov al, 3
jmp m4
m2:           ;向右上角移动
inc dl
dec dh
cmp dh, 0    ;自然需要比较有没有到顶
je d3
cmp dl, 79   ;自然需要比较有没有撞到右边
je d4
jmp m4
d3:
mov al, 0
jmp m4
d4:
mov al, 2
jmp m4
m3:        ;向左上角移动
dec dh
dec dl
cmp dh, 0
je d5
cmp dl, 0   ;同理两个边界判断
je d6
jmp m4

d5:
mov al, 4
jmp m4
d6:
mov al, 1
m4:          ;最关键过程
mov ch, dh   ;引入中间变量，记录当下坐标
mov cl, dl
mov dh, [tema]
mov dl, [temb]
mov [tema], ch
mov [temb], cl
ret

jmp $ ; 死循环




times 510 - ($ - $$) db 0
db 0x55, 0xaa

